using System;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;

namespace PMS.Common
{
    public class DesEncryption
    {
        private static string passWord;	//Encryption string of character 

        /// <summary> 
        /// Encrypt a string with default key
        /// </summary> 
        /// <param name="pToEncrypt"></param> 
        /// <returns></returns> 
        public static string Encrypt(string pToEncrypt)
        {
            return Encrypt(pToEncrypt, "8T53bfGP");
        }

        /// <summary> 
        /// Encrypt a string with your key
        /// </summary> 
        /// <param name="pToEncrypt"></param> 
        /// <param name="sKey"></param> 
        /// <returns></returns> 
        public static string Encrypt(string pToEncrypt, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();


            byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);


            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(),
            CryptoStreamMode.Write);

            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                //Format as hex 
                ret.AppendFormat("{0:X2}", b);
            }
            return ret.ToString();
        }

        /// <summary> 
        ///  Decrypt a string with default key
        /// </summary> 
        /// <param name="pToDecrypt"></param> 
        /// <returns></returns> 
        public static string Decrypt(string pToDecrypt)
        {
            return Decrypt(pToDecrypt, "8T53bfGP");
        }

        /// <summary> 
        /// Decrypt a string with your key 
        /// </summary> 
        /// <param name="pToDecrypt"></param> 
        /// <param name="sKey"></param> 
        /// <returns></returns> 
        public static string Decrypt(string pToDecrypt, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
            for (int x = 0; x < pToDecrypt.Length / 2; x++)
            {
                int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }

            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(),
            CryptoStreamMode.Write);

            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.Append((char)b);
            }
            return ret.ToString();
        }

        /// <summary>
        /// String of character encryption 
        /// </summary>
        /// <param name="PasswordString">Must encrypt string of character </param>
        /// <param name="PasswordFormat">Must encrypt category </param>
        /// <returns></returns>
        static public string EncryptPassword(string PasswordString, string PasswordFormat)
        {
            #region
            switch (PasswordFormat)
            {
                case "SHA1":
                    {
                        passWord = FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "SHA1");
                        break;
                    }
                case "MD5":
                    {
                        
                        passWord = FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "MD5");

                        break;
                    }
                default:
                    {
                        passWord = string.Empty;
                        break;
                    }
            }
            return passWord;
            #endregion
        }
    }
}
